﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZhimeiSoft.Framework.Client.Control.DateTimeGrid
{
    public delegate void TimeChange(DateTime dt);

    public partial class HourTimeSimpleControl : UserControl
    {
        private DateTime hourDate = new DateTime(DateTime.Now.Ticks);

        public TimeChange OnTimeChange;

        public HourTimeSimpleControl()
        {
            InitializeComponent();
        }

        public DateTime CurrDateTime
        {
            set
            {
                hourDate = new DateTime(value.Ticks);
                initTime(hourDate.Hour, hourDate.Minute);
            }
            get
            {

                if (cbxHour.SelectedItem != null)
                {
                    int minute = Convert.ToInt32(cbxMIn.SelectedItem.ToString());
                    hourDate = new DateTime(hourDate.Year, hourDate.Month, hourDate.Day, Convert.ToInt16(cbxHour.SelectedItem.ToString()) , minute, 1);
                }
                return hourDate;
            }
        }

        public bool SetShowTimeLable
        {
            set
            {
                labTime.Visible = value;
            }
            get
            {
                return labTime.Visible;
            }
        }

        public void initTime(int hour, int minute)
        {
            int idx = hour;
            if (idx == 0)
            {
                //0点
                cbxHour.SelectedIndex = (cbxHour.Items.Count - 1);
            }
            idx--;
            if (idx < 0)
            {
                idx = Math.Abs(idx);
            }
            
            if (cbxHour.Items.Count > idx)
            {
                cbxHour.SelectedIndex = idx;
            }
            //分钟
            idx = Math.DivRem(minute, 15, out idx);
            if (idx >= 0 && idx <= 3)
                cbxMIn.SelectedIndex = idx;
            if (idx == 4)
                cbxMIn.SelectedIndex = 0;
        }

        private void cbxHourType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (OnTimeChange != null)
            {
                OnTimeChange(this.CurrDateTime);
            }
        }
    }
}
